<?php


use Doraemon\tools\Log;
use Doraemon\model\Member;
use Doraemon\cache\Designer\DesignerRanking;
use Doraemon\model\Designer;
use Doraemon\service\DesignerRanking\Ranking;
use Doraemon\model\DesignerRanking\DesignerRanking as mDesignerRanking;
use Doraemon\service\DingTalkAlert;
use Doraemon\service\User;
use Doraemon\model\DesignerRanking\DesignerRankingConfig;
use Doraemon\service\LocalRank\PublicPraiseRank as sPublicPraiseRank;

class AutoCancelDesignerRankingAction extends ActionPlugin
{
    /**
     * @throws Exception
     */
    public function main()
    {
        Log::info('设计师自动下榜接收到事件信息', $this->messageBody);
        $message = $this->messageBody;
        $table = $this->messageBody['table'] ?? '';
        $uid = 0;
        $extend = [];
        if (strstr($table, Member::_TABLE_NAME)) {
            $uid = $this->messageBody['data']['uid'] ?? 0;
            $type = $this->messageBody['type'] ?? '';
            $user_type = $this->messageBody['data']['type'];
            $old_user_type = $this->messageBody['old']['type'];
            if ($type == 'update') {
                if ($user_type == Member::AUTH_USER_UNAUTH_DESIGNER && $old_user_type == Member::AUTH_USER_DESIGNER ) {
                    //处理下榜逻辑
                    //获取该设计师已经上榜的榜单id
                    $rankMonth = date("Ym");
                    //获取设计师的主要服务地区新地区码
                    $rankCity = Designer::getDesignerMainAreaCode($uid);
                    $mainAreaName = Designer::getDesignerSomeInfo('main_area_name',$uid);
                    $rankInfo = DesignerRanking::getDesignerRanking($uid,$rankMonth);
                    if (!empty($rankInfo)){
                        $rankIdArr = array_keys($rankInfo);
                        $rankNameArr = [];
                        foreach ($rankIdArr as $key => $value){
                            //处理榜单移除的逻辑
                            $rankName = DesignerRankingConfig::getByWhere(["id" => $value])['ranking_name'];
                            array_push($rankNameArr,$rankName);
                            $rankId = $value;
                            Ranking::removeDesignerFromRankingCache($uid,$rankMonth,$rankId,$rankCity);
                        }
                        //更新榜单数据库的状态为9,只设置当月的
                        $update_data = ["data_status" => mDesignerRanking::STATUS_DELETE];
                        mDesignerRanking::UpdateDesignerInfo($uid,$update_data,$rankMonth);

                        //获取设计师的基础信息
                        $designerUserInfo = User::getUserBaseInfo($uid);

                        //榜单信信息
                        $rankNameString = implode(",",$rankNameArr);

                        //钉钉通知
                        $msg = "设计师认证到期被自动下榜了!!\nuid:{$uid}\n设计师昵称:{$designerUserInfo['nick']}\n设计师的服务地区为:{$mainAreaName}\n已上的榜单为:{$rankNameString}\n";
                        DingTalkAlert::sendTextMsg(DingTalkAlert::RANK_ALL_NOTICE, $msg, true);

                        Log::info('接收到事件', [
                            "uid" => $uid,
                            "rankMonth" => $rankMonth,
                            "rankCity" => $rankCity,
                            "rankNameString" => $rankNameString
                        ]);
                    }
                    // 处理本地口碑榜,认证到期临时下榜
                    sPublicPraiseRank::offlineRank($uid, false, Member::AUTH_USER_DESIGNER, ['reason' => '认证到期']);
                }
             }
            $extend = compact('uid', 'user_type');
        }
        Log::info('接收到事件消息', compact('uid', 'message') + $extend);
        $this->setSuccess();
    }
}